<?php

namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\CompanyProfile;
use app\common\model\xilujob\JobApply AS JobApplyModel;
use think\Exception;

class JobApply extends XilujobApi {

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * @ApiTitle (个人简历投递记录)
     * @ApiSummary (个人主动简历投递记录)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.job_apply/apply_list)
     * @ApiHeaders (name="token", type="string", description="Token")
     */
    public function apply_list() {
        $paginator = JobApplyModel::where('job_apply.user_id', $this->auth->id)
            ->with([
                'enterpriseinfo'=>function($query){$query->withField('id,full_name,logo');},
                'job'=>function($query){$query->withField('id,city_id,town_id,job_name,salary_type,salary_min,salary_max');},
            ])
            ->order('createtime','desc')
            ->paginate();
        $applies = $paginator->getCollection();
        foreach ($applies as $apply) {
            $job = $apply->job;
            $job->relationQuery(['city'=>function($query){$query->withField('id,shortname');}]);
        }
        $this->success('查询成功', $applies);
    }


    /**
     * @ApiTitle (企业端看投递记录)
     * @ApiSummary (个人主动简历投递记录)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.job_apply/apply_list)
     * @ApiHeaders (name="token", type="string", description="Token")
     */
    public function company_list() {
        $company = CompanyProfile::get(['user_id'=>$this->auth->id]);
        $applies = JobApplyModel::where('company_id', $company->id)
            ->with([
                'resume',
                'job'=>function($query){$query->withField('id,job_name,salary_type,salary_min,salary_max');},
            ])
            ->order('createtime','desc')
            ->paginate();
        foreach ($applies as $apply) {
            $apply->resume->relationQuery(['personal']);
        }
        $this->success('', $applies);
    }


    /**
     * 企业拒绝简历投递
     * @param $id
     */
    public function reject($id) {
        $apply = JobApplyModel::get($id);
        $company = CompanyProfile::get(['user_id'=>$this->auth->id]);
        if($apply->company_id!=$company->id) {
            $this->error(__('no record'));
        }
        try {
            $ret = JobApplyModel::editApply($id, JobApplyModel::STATUS_APPLY_REJECTED);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('', ['job_apply'=>$ret]);
    }

}